﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeIT.MemCached;

namespace SDTP.Common.WfProcess.Utility
{
    /// <summary>
    /// Memory Cache的帮助类
    /// </summary>
    public class BeITMemCacheHelper
    {
        /// <summary>
        /// 用户登录的Token
        /// </summary>
        public const string SSIP_WORKFLOW_XPDL_TOKEN = "SSIP_WORKFLOW_XPDL_TOKEN";

        /// <summary>
        /// Cache实例配置名称
        /// </summary>
        private const string SSIP_WORKFLOW_MEM_CACHE_INSTANCE = "SSIP_WORKFLOW_MEM_CACHE_INSTANCE";

        /// <summary>
        /// 应用MemoryCached的TCP 端口
        /// </summary>
        private const string BEIT_MEM_CACHE_TCP_PORT = "BeITMemCacheTcpPort";

        /// <summary>
        /// 创建MemoryCached实例
        /// </summary>
        /// <returns>客户端实例</returns>
        public static MemcachedClient BuildMemoryClient()
        {
            MemcachedClient client = MemcachedClient.GetInstance(SSIP_WORKFLOW_MEM_CACHE_INSTANCE);
            if (client == null)
            {
                string memCachePort = ConfigurationManager.AppSettings[BEIT_MEM_CACHE_TCP_PORT];
                MemcachedClient.Setup(SSIP_WORKFLOW_MEM_CACHE_INSTANCE, new string[] { memCachePort });
            }
            return client;
        }

        /// <summary>
        /// 从MemoryCached获取存储的条目
        /// </summary>
        /// <param name="key">主键值</param>
        /// <returns>存储的内容值</returns>
        public static string GetKeyValueInMemoryCache(string key)
        {
            MemcachedClient client = MemcachedClient.GetInstance(SSIP_WORKFLOW_MEM_CACHE_INSTANCE);
            if (client == null)
            {
                throw new NullReferenceException("MemoryCache中没有保存该数据，或者该数据已经失效！");
            }
            object o = client.Get(key);
            return o == null ? "" : o.ToString();
        }

        /// <summary>
        /// 生成跟用户有关的身份信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>用户唯一标识</returns>
        public static string GetUniqueXmlProcessKeyName(string processGUID)
        {
            string strUniqueKey = SSIP_WORKFLOW_XPDL_TOKEN + "_" + processGUID;

            return strUniqueKey;
        }

        /// <summary>
        /// 获取当前MemoryCached的实例信息
        /// </summary>
        /// <returns></returns>
        private static MemcachedClient GetCurrentMemoryCacheInstance()
        {
            MemcachedClient client = MemcachedClient.GetInstance(SSIP_WORKFLOW_MEM_CACHE_INSTANCE);
            if (client == null)
            {
                throw new NullReferenceException("MemoryCache中没有保存该数据，或者该数据已经失效！");
            }
            return client;
        }


        /// <summary>
        /// 添加要存储到MemoryCache中的对象数据
        /// </summary>
        /// <param name="key">主键值</param>
        /// <param name="value">数据对象</param>
        /// <returns>成功是否</returns>
        public static bool AddKeyValueInMemorycache(string key, object value)
        {
            MemcachedClient client = GetCurrentMemoryCacheInstance();
            return client.Add(key, value);
        }

        /// <summary>
        /// 更改key-value
        /// </summary>
        /// <param name="key">主键值</param>
        /// <param name="value">数据对象</param>
        /// <returns>是否成功</returns>
        public static bool SetKeyValueInMemorycache(string key, object value)
        {
            MemcachedClient client = GetCurrentMemoryCacheInstance();
            return client.Set(key, value);
        }

        /// <summary>
        /// 删除key-value
        /// </summary>
        /// <param name="key">主键值</param>
        /// <returns>是否成功</returns>
        public static bool RemoveKeyValueInMemorycache(string key)
        {
            MemcachedClient client = GetCurrentMemoryCacheInstance();
            return client.Delete(key);
        }
    }
}
